// pages/widthdraw/widthdraw.js

//index.js
//获取应用实例
const app = getApp();
var route = require('../../utils/route');
var util = require('../../utils/util');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');

Page({

	/**
	 * 页面的初始数据
	 */
	data : {
		balance : null,
    amount:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad : function(options) {
    var that = this;
    app.getUserInfo(function (res) {
      that.setData({
        userInfo: res
      });
    });
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady : function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow : function() {
		this.getAccount();
	},
	getAccount : function() {
		var that = this;
		var method = route.getAccount();
		var params = {
      openId: app.globalData.openId,
		};
    debugger;
    httpClient.request(method, 'GET', params, function(res, err) {
      debugger;
			that.setData({ 
				balance : res
			});
		});

	},
	answer:function(){
		wx.navigateTo({
			url: '../question/question'
		})
	},
	/**
	 * 全部提现事件
	 */
	withdrawAmount:function(){
    this.setData({ amount: this.data.balance.totalAmount/100});
	},
	/**
	 * 提现
	 */
	withdraw:function(){
		var that = this;
		debugger;
		if(that.data.amount===''){
			wx.showToast({
				  title: '请输入金额',
				  icon: 'none',
				  duration: 3000
				})
			return;
		}
		
		if(!util.checkNumber(that.data.amount)){
			wx.showToast({
				  title: '请输入正确金额',
				  icon: 'none',
				  duration: 3000
				})
			return;
		}
    if (that.data.amount > that.data.balance.totalAmount/100){
			wx.showToast({
				  title: '余额不足',
				  icon: 'none',
				  duration: 3000
				})
			return;
		}
		if(that.data.amount <= 0){
			wx.showToast({
				  title: '余额必须大于零',
				  icon: 'none',
				  duration: 3000
				})
			return;
		}

    if (that.data.amount < 1) {
      wx.showToast({
        title: '提现金额不能小于1元',
        icon: 'none',
        duration: 3000
      })
      return;
    }
		
		var method = route.applyCashback();
		var params = {
			userId : app.globalData.userId,
			amount: that.data.amount * 100
		};
		httpClient.request(method, 'POST', params, function(res, err) {
      debugger
      
      that.setData({
        amount: ''
      })
      that.getAccount();
      wx.showToast({
        title: '提现中预计1-5个工作日到账!',
        icon: 'none',
        duration: 3000
      })
		});
	},
	getAmount:function(e){
    if (!util.checkNumber(e.detail.value)) {
      wx.showToast({
        title: '请输入正确金额',
        icon: 'none',
        duration: 3000
      })
      return;
    }
		 this.setData({
			 amount: e.detail.value
		 })
	}
})